package org.example.myleet.p467;

public class Solution {
    public int findSubstringInWraproundString(String p) {
        char[] charArr = p.toCharArray();
        int[] dp = new int[26];
        int k = 1;
        dp[charArr[0] - 'a'] = k;
        for (int i = 1; i < charArr.length; ++i) {
            int idx = charArr[i] - 'a';
            if (isNext(charArr[i - 1], charArr[i])) {
                ++k;
            } else {
                k = 1;
            }
            dp[idx] = Math.max(dp[idx], k);
        }
        int sum = 0;
        for (int x : dp) {
            sum += x;
        }
        return sum;
    }

    private boolean isNext(char prev, char cur) {
        if (prev == 'z' && cur == 'a') {
            return true;
        }
        return prev == cur - 1;
    }
}
